package com.blb.service.impl;

import com.blb.mapper.BannerDao;
import com.blb.pojo.Banner;
import com.blb.pojo.ColumnManagement;
import com.blb.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerDao bannerDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Authoe: smz
     * @Description: 根据页头位置，排序最新三个轮播图
     * @Date: 9:14 2022-01-06
     */
    @Override
    public List<Banner> findByLocation(int num) {
        String key="banner"+num;
        ValueOperations<String,List<Banner>> operations = redisTemplate.opsForValue();
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey){
            System.out.println("通过redis获取数据");
            return operations.get(key);
        }else {
            System.out.println("通过数据库获取数据");
            List<Banner> banners = bannerDao.selectByLocation(num);
            operations.set(key,banners,1, TimeUnit.MINUTES);
            return banners;
        }
    }
}
